#! /usr/bin/env python
# coding=utf-8
# ******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Author: senlin
# Create: 2021-07-04
# ******************************************************************************/

import configparser
import os
from src.libs.logger import logger
import logging

class LoadConfig(object):
    """
    """
    def __init__(self):
        """
        """
        src_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
        properties_path = src_dir + "/config/properties.conf"
        logger.info(f"properties_path: {properties_path}")
        self.conf = self.read_conf(properties_path)
        
    def read_conf(self, properties_path):
        """
        args:
            properties_path: path of configuration file
        returns:
            config: the object of configparser.ConfigParser()
        """
        if not os.path.isfile(properties_path):
            logger.error(f"Invalid file path: {properties_path}")
            return None
        try:
            config = configparser.ConfigParser()
            config.read(properties_path, encoding='utf-8')
            return config
        except configparser.MissingSectionHeaderError or UnicodeDecodeError as err:
            logger.error(f"please check the decoding mode：{err.message}")
            return None
        except configparser.NoSectionError as err:
            logger.error(f"{properties_path} is invalid!")
            return None

    def get_section_key_value(self, section, key):
        """
        Gets the value of the key in the specified section

        args:
            section: section
            key: key

        returns:
            value: the value of the key in the specified section
        """
        try:
            value = self.conf.get(section, key)
            return value
        except AttributeError or KeyError as err:
            logger.error(f"reason: {err}")
            return "NA"
        except configparser.NoOptionError or configparser.NoSectionError as err:
            logger.error(f"reason: {err.message}")
            return "NA"

local_conf = LoadConfig()
""" 
if __name__ == "__main__":
    sig_query_conf = dict(local_conf.conf.items("sig_query_conf"))
    sigs_yaml_url = sig_query_conf.get('sigs_yaml_url')
    logging.info(f"{sigs_yaml_url}") """